package cn.cdut.yiwenqian.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import cn.cdut.yiwenqian.dao.interfaces.ITitleTypeDao;
import cn.cdut.yiwenqian.entity.CdutPostTitletypes;
@Scope("singleton")
@Repository
public class TitleTypeDaoImpl extends HibernateDaoSupport implements ITitleTypeDao {

	@Resource
	public void setMySessionFactory(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	@Override
	public CdutPostTitletypes findTypeById(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<CdutPostTitletypes> findAllChildType(String type) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void saveType(CdutPostTitletypes type) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateType(CdutPostTitletypes type) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public CdutPostTitletypes findTypeByName(final String name) {
		List<CdutPostTitletypes> list=this.getHibernateTemplate().execute( new HibernateCallback<List<CdutPostTitletypes>>(){

			@Override
			public List<CdutPostTitletypes> doInHibernate(Session session) throws HibernateException {
				Query query=session.createQuery("from CdutPostTitletypes tt where tt.name=?");
				query.setString(0, name);
				return query.list();
			}});
		if(list.size()>0){
			return  list.get(0);
		}else{
			return null;
		}
	}
	@Override
	public List<CdutPostTitletypes> findTypeByKeyword(final String keyword) {
		List<CdutPostTitletypes> list=this.getHibernateTemplate().execute( new HibernateCallback<List<CdutPostTitletypes>>(){

			@Override
			public List<CdutPostTitletypes> doInHibernate(Session session) throws HibernateException {
				Query query=session.createQuery("from CdutPostTitletypes tt where tt.name like ?");
				query.setString(0, keyword);
				return query.list();
			}});
		return list;
	}

}
